import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * Created with Intellij JDEA
 * Description:
 * User:
 * Date:2022-06-07
 * Time:15:36
 */
public class 最小的K个数 {
    public int[] getLeastNumbers(int[] arr, int k) {
        int[] ret = new int[k];
        if(k == 0) {
            return ret;
        }
        PriorityQueue<Integer> p = new PriorityQueue<>(new Comparator<Integer>() {
            public int compare (Integer o1,Integer o2) {
                return o2-o1;
            }
        });
        for(int i = 0;i < k;i++) {
            p.offer(arr[i]);
        }
        for(int i = k;i < arr.length;i++) {
            if(p.peek() > arr[i]) {
                p.poll();
                p.offer(arr[i]);
            }
        }
        for(int i = 0;i < k;i++) {
            ret[i] = p.poll();
        }
        return ret;
    }

}
